// pages/qichachaPage/qichachaPage.js
import * as echarts from '../../components/ec-canvas/echarts';
import {
  request
} from '../../request/index.js';
var app = getApp()
let PieChart = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyList: [
      '公司名称', '电话', '官网', '邮箱', '地址', '登记机关', '注册资本', '企业规模', '实缴资本', '简介',
      '董事长', '董事', '独立董事', '监事', '法人代表', '证券事务代表', '历史承建项目'
    ],
    markerDetailInfo: {

    },
    keyword: '',
    companyInfo: {
      Name: '', // 公司名称
      OperName: '', // 法人代表
      RegistCapi: '', // 注册资本
      Address: '', // 公司地址
      Scope: '', // 营业范围
      BelongOrg: '', // 注册地址
      PersonScope: '', // 人员规模
      RecCap: '', // 实缴资本

      Partners: [], // 股权结构
      Employees: [], // 人员信息
      ContactInfo: { // 联系信息
        WebSite: [{
          Url: ''
        }],
        PhoneNumber: '',
        Email: ''
      },
      Area: { // 地区信息
        Province: '',
        City: '',
        County: ''
      }
    },
    companyName: '',
    project: [],
    flag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.echart_Pie = this.selectComponent('#mychart-dom-pie');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  category(type) {
    /* eslint-disable-next-line */
    if (type == '公司') {
      return 0
    }
    return 1
  },
  bindkeyword(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  chairman: function () {
    let res = ''
    if (this.data.companyInfo.Employees) {
      for (let i in this.data.companyInfo.Employees) {
        /* eslint-disable-next-line */
        if (this.data.companyInfo.Employees[i].Job == '董事长') {
          res += this.data.companyInfo.Employees[i].Name
          res += ' '
        }
      }
    }
    return res
  },
  secretary: function () {
    let res = ''
    if (this.data.companyInfo.Employees) {
      for (let i in this.data.companyInfo.Employees) {
        /* eslint-disable-next-line */
        if (this.data.companyInfo.Employees[i].Job == '董事') {
          res += this.data.companyInfo.Employees[i].Name
          res += ' '
        }
      }
    }
    return res
  },
  independentDirector: function () {
    let res = ''
    if (this.data.companyInfo.Employees) {
      for (let i in this.data.companyInfo.Employees) {
        /* eslint-disable-next-line */
        if (this.data.companyInfo.Employees[i].Job == '独立董事') {
          res += this.data.companyInfo.Employees[i].Name
          res += ' '
        }
      }
    }
    return res
  },
  supervisor: function () {
    let res = ''
    if (this.data.companyInfo.Employees) {
      for (let i in this.data.companyInfo.Employees) {
        /* eslint-disable-next-line */
        if (this.data.companyInfo.Employees[i].Job == '监事') {
          res += this.data.companyInfo.Employees[i].Name
          res += ' '
        }
      }
    }
    return res
  },
  getPieOption: function () {
    this.data.nodes = [{
      name: this.data.companyInfo.Name,
      category: 0, // 0代表公司，1代表自然人股东
      value: 100,
      capi: this.data.companyInfo.RegistCapi
    }]
    // console.log(this.data.companyInfo.Partners)
    if (this.data.companyInfo.Partners) {
      this.setData({
        flag: true
      })
      this.data.links = []
      for (var i = 0; i < this.data.companyInfo.Partners.length; i++) {
        this.data.nodes.push({
          name: this.data.companyInfo.Partners[i].StockName,
          category: this.category(this.data.companyInfo.Partners[i].StockType), // 0代表公司，1代表自然人股东
          value: this.data.companyInfo.Partners[i].StockPercent,
          capi: this.data.companyInfo.Partners[i].ShouldCapi
        })
        this.data.links.push({
          source: this.data.companyInfo.Partners[i].StockName,
          target: this.data.companyInfo.Name,
          name: this.data.companyInfo.Partners[i].StockPercent,
          capi: this.data.companyInfo.Partners[i].StockPercent
        })
      }
      const color1 = '#006acc'
      const color2 = '#ff7d18'
      const color3 = '#000000D6'
      this.data.nodes.forEach(node => {
        if (node.category === 0) {
          // 控制大圆的大小
          node.symbolSize = node.value * 1.05 + 71

          node.itemStyle = {
            color: color1
          }
        }
      })
      this.data.links.forEach(link => {
        link.label = {
          align: 'center',
          fontSize: 12
        }

        link.lineStyle = {
          color: color3
        }
      })
      let categories = [{
          name: '公司',
          itemStyle: {
            color: color1
          }
        },
        {
          name: '自然人股东',
          itemStyle: {
            color: color2
          }
        }
      ]
      let option = {
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            var color = params.color // 图例颜色
            var htmlStr = '<div>'
            htmlStr += params.name + '<br/>' // x轴的名称

            // 为了保证和原来的效果一样，这里自己实现了一个点的效果
            htmlStr += '<span style="margin-right:5px;display:inline-block;width:10px;height:10px;border-radius:5px;background-color:' + color + ';"></span>'

            // 显示持股数（capi字段）
            htmlStr += params.data.capi

            htmlStr += '</div>'

            return htmlStr
          },

          itemPointer: {
            type: 'shadow'
          }
        },
        // title: {
        //   text: '知识图谱',
        // },
        legend: [{
          // selectedMode: 'single',
          textStyle: { // 图例文字的样式
            color: 'black'
          },
          data: categories.map(x => x.name)
          // icon: 'circle'
        }],
        series: [{
          type: 'graph',
          layout: 'force',

          // 控制小圆的大小
          symbolSize: 7 + 50,
          // data.nodes.forEach(nodeValue),
          draggable: true,
          roam: true,
          focusNodeAdjacency: true,
          categories: categories,
          edgeSymbol: ['', 'arrow'],
          // edgeSymbolSize: [80, 10],
          edgeLabel: {
            normal: {
              show: true,
              textStyle: {
                fontSize: 18
              },
              formatter(x) {
                return x.data.name
                // return x.data.capi;
              }
            }
          },
          label: {
            show: true
          },
          force: {
            repulsion: 2000, // 控制箭头线的长度
            edgeLength: 120
          },
          data: this.data.nodes,
          links: this.data.links

        }]

      }
      return option;
    }
  },
  initEchartPie: function () {
    this.echart_Pie.init((canvas, width, height, dpr) => {
      PieChart = echarts.init(canvas, null, {
        width: width,
        height: height,
        devicePixelRatio: dpr // new
      });
      canvas.setChart(PieChart);
      if(this.getPieOption()){  
        PieChart.setOption(this.getPieOption());
        return PieChart;
      }
    })
  },
  async searchCompany() {
    let res = await request({
      url: '/qichacha/keyword?name=' + this.data.keyword
    })
    if (res) {
      this.initEchartPie()
      this.setData({
        companyInfo: res.data.data.information,
        companyName: res.data.data.name,
        project: res.data.data.project
      })
      // 生成键值对
      let tmp = {}
      this.data.keyList.map(v => {
        switch (v) {
          case '公司名称':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.Name : "——"
            break;
          case '电话':
            tmp[v] = (this.data.companyInfo && this.data.companyInfo.ContactInfo) ? this.data.companyInfo.ContactInfo.PhoneNumber : "——"
            break;
          case '官网':
            tmp[v] = (this.data.companyInfo && this.data.companyInfo.ContactInfo && this.data.companyInfo.ContactInfo.WebSite && this.data.companyInfo.ContactInfo.WebSite[0]) ? this.data.companyInfo.ContactInfo.WebSite[0].Url : '——'
            break;
          case '邮箱':
            tmp[v] = (this.data.ContactInfo) ? this.data.ContactInfo.Email : "——"
            break;
          case '地址':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.Address : "——"
            break;
          case '登记机关':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.BelongOrg : "——"
            break;
          case '注册资本':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.RegistCapi : "——"
            break;
          case '企业规模':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.PersonScope : "——"
            break;
          case '实缴资本':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.RecCap : "——"
            break;
          case '简介':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.Scope : "——"
            break;
          case '董事长':
            tmp[v] = this.chairman()
            break;
          case '董事':
            tmp[v] = this.secretary()
            break;
          case '独立董事':
            tmp[v] = this.independentDirector()
            break;
          case '监事':
            tmp[v] = this.supervisor()
            break;
          case '法人代表':
            tmp[v] = (this.data.companyInfo) ? this.data.companyInfo.OperName : "——"
            break;
          case '证券事务代表':
            tmp[v] = ''
            break;
        }
      })
      this.setData({
        markerDetailInfo: tmp,
        flag: false
      })
    }
  }
})